//
//  WDBMSManager.m
//  WDPolymerizeSDK
//
//  Created by little People on 2023/6/2.
//

#import "WDBMSManager.h"
#import "WDNSLog.h"
#import "WDPolymerizeSDK.h"
#import "WDJsonManager.h"

@implementation WDBMSManager
+ (void)WDGetWithVersion:(NSString *)ver appName:(NSString *)appName completionHandler:(void (^)(BOOL isSuccess, WDBMSModel *model))completionHandler
{
    NSString *string = [NSString stringWithFormat:@"https://game.zuiqiangyingyu.net/common/config/info?version=%@&app_name=%@",[WDJsonManager BMSVersionString],[WDJsonManager requestAppNameForSDKConfigFile]];
    //1.创建URL对象

    NSURL *url = [NSURL URLWithString:string];
    //2.建立会话 session支持三种类型的任务
    //    NSURLSessionDataTask  //加载数据
    //    NSURLSessionDownloadTask  //下载
    //    NSURLSessionUploadTask   //上传
    NSURLSession *session = [NSURLSession sharedSession];
    //3.创建请求任务，（该方法内部默认使用get）直接进行传递url即可
    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        //data就是你获取到的数据，你可以自行对其进行解析
        //error就是获取失败时出现的问题
        if (error)
        {
            completionHandler(NO,nil);
        }
        else
        {
            if (!data)
            {
                completionHandler(NO,nil);
                return;
            }
            NSDictionary *content = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];//转换数据格式

            if (content.count <= 0)
            {
                completionHandler(NO,nil);
                return;
            }
            [WDBMSModel shareBMSModel].dataDict = content;
            
            NSString *code = [content objectForKey:@"code"];
            if (![code boolValue])
            {
                NSDictionary *dataDict = [content objectForKey:@"data"];
                if (dataDict.count <= 0)
                {
                    completionHandler(NO,nil);
                    return;
                }
                WDBMSModel *model = [WDBMSModel shareBMSModel];
                model.hasBMSContent = YES;
                model.GM = [[dataDict allKeys] containsObject:@"GM"]?[dataDict objectForKey:@"GM"]:@"";
                model.TLS = [[dataDict allKeys] containsObject:@"TLS"]?[dataDict objectForKey:@"TLS"]:@"";
                model.interConfig = [[dataDict allKeys] containsObject:@"INTERCONFIG"]?[dataDict objectForKey:@"INTERCONFIG"]:@[];
                model.splashConfig = [[dataDict allKeys] containsObject:@"SPLASHCONFIG"]?[dataDict objectForKey:@"SPLASHCONFIG"]:@[];
                model.cardConfig = [[dataDict allKeys] containsObject:@"CARDCONFIG"]?[dataDict objectForKey:@"CARDCONFIG"]:@[];
                model.interList = [[dataDict allKeys] containsObject:@"INTERLIST"]?[dataDict objectForKey:@"INTERLIST"]:@[];
                model.rvList = [[dataDict allKeys] containsObject:@"RVLIST"]?[dataDict objectForKey:@"RVLIST"]:@[];
                model.cardList = [[dataDict allKeys] containsObject:@"CARDLIST"]?[dataDict objectForKey:@"CARDLIST"]:@[];
                model.LTVOneDayList = [[dataDict allKeys] containsObject:@"LTVOneDayList"]?[dataDict objectForKey:@"LTVOneDayList"]:@[];
                NSArray *splashIDs = [[dataDict allKeys] containsObject:@"SplashID"]?[dataDict objectForKey:@"SplashID"]:@[];
                NSArray *NativeIDs = [[dataDict allKeys] containsObject:@"NativeID"]?[dataDict objectForKey:@"NativeID"]:@[];
                NSArray *PromotionWeights = [[dataDict allKeys] containsObject:@"PromotionWeight"]?[dataDict objectForKey:@"PromotionWeight"]:@[];
                
                model.splashID = [NSArray arrayWithArray:[self bubblingArray:splashIDs]];
                model.nativeID = [NSArray arrayWithArray:[self bubblingArray:NativeIDs]];
//                model.promotionWeight = [NSArray arrayWithArray:[self bubblingArray:PromotionWeights]];
                WDLog(@"BMS-GM=%@ 1==%@,2==%@,3==%@",model.GM,model.splashID,model.nativeID,model.promotionWeight);

                completionHandler(YES,model);
                
            }
        }
        
    }];
    //4.启动任务
    [dataTask resume];
}

+ (NSMutableArray *)bubblingArray:(NSArray *)dataArray
{
    
    NSMutableArray *temparray = [NSMutableArray array];
    if (dataArray.count > 0)
    {
        [temparray addObjectsFromArray:dataArray];
        for (int i = 0; i < temparray.count - 1; i++) {
            for (int j = 0; j < temparray.count - 1; j++) {
                NSArray *array = temparray[j];
                NSArray *array1 = temparray[j+1];
                if (array.count == 2 && array1.count == 2)
                {
                    float a = [array.firstObject intValue];
                    float b = [array1.firstObject intValue];
                    if (a < b)
                    {
                        NSArray *c = temparray[j];
                        temparray[j] =  temparray[j+1];
                        temparray [j+1] = c;
                    }
                }
                
            }
        }
        WDLog(@"排序后的数组===%@",temparray);
    }
    
    return temparray;
}

@end
